<template>
  <div>
    <el-form ref="form">
      <el-form-item prop="search" class="mb20 mt10">
        <el-col :span="4">
          <el-button type="primary" @click="handleAdd" icon="el-icon-circle-plus-outline">添加包</el-button>
        </el-col>
        <el-row type="flex" class="row-bg" justify="end">
          <el-col :xs="12" :sm="10" :md="8" :lg="6">
            <el-input
              v-model="search"
              size="mini"
              placeholder="输入分配时间搜索"
              @keyup.enter.native="getPackages(15, 0)"
            ></el-input>
          </el-col>
          <el-col :xs="8" :sm="6" :md="4" :lg="2">
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-search"
              @click="getPackages(15, 0)"
            >搜索</el-button>
          </el-col>
          <el-col :xs="8" :sm="6" :md="4" :lg="2">
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-circle-close-outline"
              @click="resetSearch"
            >重置</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
    <packageDialog
      :title="title"
      :addDialog="addDialog"
      :editDialog="editDialog"
      :packageForm="packageForm"
      @close="hanleClose"
      @update="handleUpdate"
    ></packageDialog>
    <el-table
      v-loading="loading"
      border
      element-loading-text="拼命加载中"
      :data="tableData"
      style="width: 100%"
    >
      <el-table-column type="selection" width="45"></el-table-column>
      <el-table-column prop="id" label="序号" width="65"></el-table-column>
      <el-table-column sortable prop="createdAt" label="创建时间" width="140">
        <template slot-scope="scope">{{scope.row.createdAt | moment("YYYY/MM/DD h:mm:ss")}}</template>
      </el-table-column>
      <el-table-column sortable prop="time" label="分配时间" width="95">
        <template slot-scope="scope">{{scope.row.time | moment("YYYY-MM-DD")}}</template>
      </el-table-column>
      <el-table-column prop="centerName" label="中心名" width="110"></el-table-column>
      <el-table-column prop="deptName" label="部门名" width="140"></el-table-column>
      <el-table-column prop="promotionName" label="渠道名" width="120"></el-table-column>
      <el-table-column prop="cost" label="花费(元)" width="80"></el-table-column>
      <el-table-column prop="amount" label="数量(个)" width="80"></el-table-column>
      <el-table-column prop="operation" label="操作" width="200">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            @click="handleEdit(scope.row)"
            icon="el-icon-edit-outline"
          >编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row.id)"
            icon="el-icon-delete"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="mt20">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="pages"
        :page-size="100"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </div>
  </div>
</template>
<script>
import packageDialog from "@/components/package_dialog";

export default {
  name: "package",
  data() {
    return {
      search: "",
      loading: true,
      title: "",
      total: 0,
      page: 15,
      val: 1,
      currentPage: 1,
      pages: [15, 20, 25, 35],
      tableData: [],
      addDialog: false,
      editDialog: false,
      packageForm: {}
    };
  },
  created() {
    this.getPackages(15, 0);
  },
  methods: {
    handleSizeChange(val) {
      this.page = val;
      this.getPackages(val, 0);
    },
    handleCurrentChange(val) {
      this.val = val;
      // 下一页页数*每页显示行-上一页页数*每页显示行
      this.getPackages(this.page, (val - 1) * this.page);
    },
    getPackages(limit, offset) {
      this.$axios
        .get(
          `/api/package/packages/?limit=${limit}&offset=${offset}&search=${
            this.search
          }`
        )
        .then(data => {
          this.tableData = data.doc;
          this.total = data.total;
          this.loading = false;
        });
    },
    handleAdd() {
      this.addDialog = !this.addDialog;
      this.title = "添加包";
    },
    hanleClose() {
      this.addDialog = false;
      this.editDialog = false;
      this.resetForm();
    },
    handleEdit(item) {
      this.editDialog = !this.editDialog;
      this.title = "编辑包";
      this.packageForm = item;
    },
    resetForm() {
      this.packageForm = {
        time: "",
        promotion: "",
        cost: "",
        amount: "",
        center: "",
        dept: ""
      };
    },
    handleDelete(id) {
      this.$confirm("此操作将删除该条包, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$axios.delete("/api/package/removePackage/" + id).then(data => {
            this.$message({
              type: "success",
              message: "删除成功!"
            });
            this.handleUpdate();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    resetSearch() {
      this.search = "";
      this.getPackages(15, 0);
    },
    handleUpdate() {
      this.getPackages(this.page, (this.val - 1) * this.page);
    }
  },
  components: {
    packageDialog
  }
};
</script>
<style>
</style>
